package com.example.dps;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.database.Cursor;
import android.util.Log;

public class Transfer {
	
	public int id;
	
	public int amount;
	
	public String frequency;
	
	public int interval;
	
	public String from;
	
	public String to;
	
	public Date starting;
	
	public Date ending;
	
	public Date lastTransfer;
	
	public Date nextTransfer;
	
	public boolean enabled;
	
	Transfer(Cursor cursor) throws ParseException{
		
		
		
		int id = cursor.getInt(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_ID));
		
		String frequency = cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_FREQUENCY));
		String from = cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_FROM));
		String to = cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_TO));
		
		Date starting =  new SimpleDateFormat("MM/dd/yyyy").parse(cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_STARTING)));
		Date ending =  new SimpleDateFormat("MM/dd/yyyy").parse(cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_ENDING)));
		Date last_transfer = null;
		try{
			last_transfer = new SimpleDateFormat("MM/dd/yyyy").parse(cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_LAST_AUTO_TRANSFER)));
		}
		catch(ParseException e){
			
		}
		
//		
//		
//		if(starting.compareTo(new Date()) <= 0){
//			last_transfer =  new SimpleDateFormat("MM/dd/yyyy").parse(cursor.getString(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_STARTING)));
//		}

		int enabled = cursor.getInt(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_ENABLED));
		int amount = cursor.getInt(cursor.getColumnIndex(TransferDBHelper.COLUMN_TRANSFER_AMOUNT));
		
		
		
		this.id = id;
		this.frequency = frequency;
		this.from = from;
		this.to = to;
		
		this.starting = starting;
		this.ending = ending;
		this.lastTransfer = last_transfer;
		
		this.amount = amount;
		
		/** convert from int to bool **/
		if(enabled == 0) this.enabled = false;
		else this.enabled = true;
		
		int yearDays = 365;
		int halfYearDays = 182;
		int monthDays = 30;
		int halfMonthDays = 15;
		int weekDays = 7;
		
		if(this.frequency.toLowerCase().equals("daily")) this.interval = 1;
		else if(this.frequency.toLowerCase().equals("weekly")) this.interval = weekDays;
		else if (this.frequency.toLowerCase().equals("bimonthly")) this.interval=halfMonthDays;
		else if (this.frequency.toLowerCase().equals("monthly")) this.interval=monthDays;
		else if (this.frequency.toLowerCase().equals("half-yearly")) this.interval=halfYearDays;
		else if (this.frequency.toLowerCase().equals("yearly")) this.interval=yearDays;
		else this.interval = 0;
		
		/** get the next transfer date **/
		Calendar calendar = Calendar.getInstance();
		if(lastTransfer == null){
			/** if starting transfer is in the future, the next transfer = starting **/
			Log.e("Transfer", "Last transfer == null");
			this.nextTransfer = this.starting;
		}
		else{
			/** if deposit has already started, then next deposit = interval + lastTransfer date **/
			calendar.setTime(lastTransfer);
			calendar.add(Calendar.DAY_OF_YEAR, interval);
			this.nextTransfer = calendar.getTime();
			
			Log.e("Transfer", "In Constructor, nextTransfer = " + this.nextTransfer.toLocaleString());
		}
		
	
	}

	public void removeFromDatabase(TransferDBHelper helper) {
		// TODO Auto-generated method stub
		
		helper.delete(this.id);
		
		
	}

//	public void updateLastTransfer() {
//		
//		Log.e("Transfer" , " UPDATING LAST TRANSFER ********");
//		
//		Calendar calendar = Calendar.getInstance();
//		
//		calendar.setTime(this.lastTransfer);
//		calendar.add(Calendar.DAY_OF_YEAR, this.interval);
//		
//		this.lastTransfer = calendar.getTime();
//		
//		Log.e("Transfer", lastTransfer.toLocaleString());
//		
//		
//		
//	}
	
	/** 
	 * updates the next transfer, according to the last transfer and the frequency
	**/
	public void updateNextTransfer(){

		
		/** get the next transfer date **/
		Calendar calendar = Calendar.getInstance();
		if(lastTransfer == null){
			/** if starting transfer is in the future, the next transfer = starting **/
			this.nextTransfer = this.starting;
		}
		else{
			/** if deposit has already started, then next deposit = interval + lastTransfer date **/
			calendar.setTime(lastTransfer);
			calendar.add(Calendar.DAY_OF_YEAR, this.interval);
			this.nextTransfer = calendar.getTime();
			
			Log.e("Transfer", "New Next Transfer == " + this.nextTransfer.toLocaleString());
		}
		
	}

}

